package sk.marek.barak.client.util;

import sk.marek.barak.data.NewAnnoucement;
import sk.marek.barak.data.OneDay;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;

import org.eclipse.swt.widgets.MessageBox;


import com.ibm.icu.util.Calendar;

@SuppressWarnings("unchecked")
public class OneDayUtil {
	private static Map<String,OneDay> month;
	private static String curentDay;
	private static String curentMonth;
	private static String curentYear;
	
	static{
		month = null;
		Calendar calendar;
		ObjectInputStream input = null;
		calendar = Calendar.getInstance();
		Date today = calendar.getTime();
		SimpleDateFormat todayday = new SimpleDateFormat("dd");
		SimpleDateFormat todaymonth = new SimpleDateFormat("MM");
		SimpleDateFormat todayyear = new SimpleDateFormat("YYYY");
		
		curentDay = todayday.format(today);
		curentMonth = todaymonth.format(today);
		curentYear = todayyear.format(today);
		
		File f = new File("."+ File.separator+"Data" );
		if(!f.exists())
			f.mkdir();
		f =new File(f.getAbsolutePath()+File.separator +curentYear);
		if(!f.exists())
			f.mkdir();
		f = new File(f.getAbsolutePath()+File.separator+curentMonth);
		
		try {
			input = new ObjectInputStream(new FileInputStream(f));
			month = (HashMap<String,OneDay>)input.readObject();
		} catch (FileNotFoundException e) {
			month = new HashMap<String,OneDay>();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static OneDay getCurentDay(){
		if(month.containsKey(curentDay))
			saveDay(new OneDay());
		return month.get(curentDay);
		
	};	
	public static void saveDay(OneDay day){
			month.put(curentDay, day);
	}
}